<template>
	<view class="login-container">
		<!-- Logo区域 -->
		<view class="logo-area">
			<image src="/static/logo.png" mode="aspectFit" class="logo"></image>
			<text class="app-name">欢迎访问CRM</text>
		</view>

		<!-- 表单区域 -->
		<view class="form-area">
			<view class="input-group">
				<text class="input-label">账号</text>
				<input class="input-box" placeholder="请输入账号" placeholder-class="placeholder" v-model="username" />
			</view>

			<view class="input-group">
				<text class="input-label">密码</text>
				<input class="input-box" placeholder="请输入密码" placeholder-class="placeholder" password
					v-model="password" />
			</view>
		</view>

		<!-- 登录按钮 -->
		<button class="login-btn" @click="handleLogin" :disabled="!username || !password">登 录</button>
	</view>
</template>

<script>
	import CryptoJS from 'crypto-js'
	export default {
		data() {
			return {
				username: '',
				password: ''
			}
		},
		methods: {
			encrypt(word) {
				const time = Date.now()
	
				const key = CryptoJS.enc.Utf8.parse('-mall4j-password')
				const srcs = CryptoJS.enc.Utf8.parse(time + word) // 加密方式: 时间戳 + 密文
				const encrypted = CryptoJS.AES.encrypt(srcs, key, {
					mode: CryptoJS.mode.ECB,
					padding: CryptoJS.pad.Pkcs7
				})
				return encrypted.toString()
			},
			async handleLogin() {
				// 这里添加登录逻辑
				uni.showToast({
					title: '登录中...',
					icon: 'loading'
				})
				const result = await this.$api.user.loginAcc({
					userName: this.username,
					passWord: this.encrypt(this.password)
				});
				console.log('result:', result);
				if(result){
					uni.showToast({
						title: '登录成功',
						icon: 'success'
					})
					uni.setStorageSync("THCRMTOKEN",result.accessToken)
					uni.setStorageSync("THCRMUSERCODE",result.userCode)
					uni.setStorageSync("THCRMUSERNAME",result.empName)
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		padding: 0 60rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		height: 100vh;
		background: linear-gradient(to bottom, #f5f7fa, #e4e8f0);
	}

	.logo-area {
		margin-top: 120rpx;
		margin-bottom: 80rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.logo {
			width: 180rpx;
			height: 180rpx;
			border-radius: 50%;
			box-shadow: 0 10rpx 20rpx rgba(0, 0, 0, 0.1);
		}

		.app-name {
			margin-top: 30rpx;
			font-size: 36rpx;
			color: #333;
			font-weight: bold;
		}
	}

	.form-area {
		width: 100%;

		.input-group {
			margin-bottom: 50rpx;

			.input-label {
				display: block;
				margin-bottom: 20rpx;
				font-size: 28rpx;
				color: #666;
			}

			.input-box {
				width: 100%;
				height: 90rpx;
				padding: 0 30rpx;
				border-radius: 45rpx;
				background: #fff;
				box-shadow: 0 10rpx 20rpx rgba(0, 0, 0, 0.05);
				font-size: 30rpx;
				width: 580rpx;
			}
		}
	}

	.login-btn {
		margin-top: 80rpx;
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 45rpx;
		background: linear-gradient(to right, #4facfe, #00f2fe);
		color: #fff;
		font-size: 34rpx;
		box-shadow: 0 10rpx 20rpx rgba(79, 172, 254, 0.3);

		&[disabled] {
			opacity: 0.6;
		}
	}

	.placeholder {
		color: #ccc;
		font-size: 28rpx;
	}
</style>